﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
namespace ExampleWin
{
    /// <summary>
    /// tcp服务器端 非socket
    /// </summary>
    public partial class SocketServer : Form
    {
        TcpListener listen = null;
        Thread thread = null;
        public SocketServer()
        {
            InitializeComponent();
        }

        private void btnStartService_Click(object sender, EventArgs e)
        {
            try
            {
                btnStopService.Enabled = true;
                btnStartService.Enabled = false;

                //创建新的进程进行监听
                thread = new Thread(new ThreadStart(listening));
                thread.Start();
            }
            catch (Exception ex)
            {
                textBox1.AppendText(ex.Message);
            }
        }

        private void listening()
        {
            int port = 234;
            string ip = "127.0.0.1";
            //IPHostEntry host =  Dns.GetHostEntry("192.168.0.107");
            //IPAddress[] address =  host.AddressList;
            IPAddress ipaddress = IPAddress.Parse(ip);
            listen = new TcpListener(ipaddress, port);
            //1.启动监听绑定
            listen.Start();

            //无限循环进行监听
            while (true)
            {
                //2.接受挂起 等待客户端返回
                var client = listen.AcceptTcpClient();

                NetworkStream stream = client.GetStream();

                //3.读取客户端的数据
                byte[] data = new byte[1024];
                int index = stream.Read(data, 0, data.Length);
                string text = Encoding.UTF8.GetString(data, 0, index);
                textBox1.AppendText(text);

                //4.返回给客户端数据
                data = Encoding.UTF8.GetBytes(text + DateTime.Now);
                stream.Write(data, 0, data.Length);

                stream.Close();
                client.Close();
            }

            ////3.读取客户端的数据
            //StreamReader sr = new StreamReader(stream);
            //var text = sr.ReadToEnd();
            //textBox1.AppendText(text);

            ////4.返回给客户端数据
            //StreamWriter sw = new StreamWriter(stream);
            //sw.Write(text + " 哈哈，我收到了" + DateTime.Now);
            //sw.Close();
            //sr.Close();
        }

        private void btnStopService_Click(object sender, EventArgs e)
        {
            thread.Abort();
            listen.Stop();


            btnStopService.Enabled = false;
            btnStartService.Enabled = true;
        }

        private void SocketServer_Load(object sender, EventArgs e)
        {
            btnStopService.Enabled = false;
            btnStartService.Enabled = true;
        }

        private void btnExist_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void SocketServer_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (thread != null && thread.ThreadState != ThreadState.Aborted)
                thread.Abort();
           

        }
    }
}
